package com.gzhryc.html;

import java.sql.SQLException;
import java.util.List;

import com.gzhryc.jdbc.MultiDBTools;
import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.ClassTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.html.dao.WebsiteDao;
import com.gzhryc.html.dao.db.Website;
import com.gzhryc.html.dao.enums.WebsiteEnum;
import com.gzhryc.jdbc.Conditions;

public class WebsiteService {

	public static WebsiteService self() {
		return MultiDBTools.getService(WebsiteService.class, true);
	}

	public static WebsiteService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey,WebsiteService.class, true);
	}

	WebsiteDao dao;

	public WebsiteService() {
		dao = new WebsiteDao(null);
	}

	public WebsiteService(String jdbcKey) {
		this.dao = new WebsiteDao(jdbcKey);
	}


	public List<Website> find(Website website){
		Conditions condition = new Conditions(Website.class);
		if(website != null){
			condition.and().is("name", website.getName());
			condition.and().likeBoth("title", website.getTitle());
			condition.and().is("state", website.getState());
		}
		try {
			return dao.findByCondition(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Long count(Website website){
		Conditions condition = new Conditions(Website.class);
		if(website != null){
			condition.and().is("name", website.getName());
			condition.and().likeBoth("title", website.getTitle());
			condition.and().is("state", website.getState());
		}
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public List<Website> findByStatus(Integer state){
		Conditions condition = new Conditions(Website.class);
		condition.and().is("state", state);
		try {
			return dao.findByCondition(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Website getById(Long id) {
		try {
			return dao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Website getByName(String name){
		if(StringUtils.isNotBlank(name)){
			Conditions condition = new Conditions(Website.class);
			condition.and().is("name", name);
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	
	public List<Website> findByHtmlTheme(String htmlTheme){
		if(StringUtils.isNotBlank(htmlTheme)){
			Conditions condition = new Conditions(Website.class);
			condition.and().is("htmlTheme", htmlTheme);
			try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public boolean update(Website website) throws LogicException {
		if(website.getId() != null) {
			Website temp = getByName(website.getName());
			if(temp != null) {
				if(website.getId() != null && !temp.getId().equals(website.getId())) {
					throw new LogicException("名称重复");
				}
			}
			
			if(temp == null) {
				temp = getById(website.getId());
			}
			if(temp != null) {
				temp = ClassTools.copyNotNull(temp, website);
				try {
					return dao.updateNotNull(temp) > 0;
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	
	public boolean add(Website website) throws LogicException{
		Website temp = getByName(website.getName());
		if(temp != null) {
			throw new LogicException("名称重复");
		}
		if(website.getState() == null) {
			website.setState(WebsiteEnum.State.Usable.index());
		}
		try {
			return dao.insert(website) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean deleteById(Long id) {
		try {
			return dao.deleteById(id) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
